<template>
  <div>
    <el-row>
      <div style="margin-right: 800px;">
        <div class="collect">
          <el-button type="success" size="mini" style="margin-right: 1000px;margin-bottom: 10px;"
            @click="toStrategy">返回</el-button>
        </div>

        <el-table-column width="620">
          <el-row>

          </el-row>
        </el-table-column>
      </div>

      <el-col :span="16">
        <div class="grid-content bg-purple">
          <el-card class="box-card" style="width: 100%;">
            <div slot="header" class="clearfix">
              <div>
                <el-image :src="userInfo.icon"
                  style=" width: 50px; height: 50px; border-radius: 100px;vertical-align: middle;"></el-image>

                <span style="font-size: 25px; font-weight: bold; color:dodgerblue; margin: 0px 5px;">
                  {{ userInfo.userName }}</span>

                <span style="font-size: 13px;  margin-left: 10px; margin-top: 0px;  margin-bottom: 0px;"><i>发表于：</i>{{
                  userInfo.addTime }}</span>
              </div>
              <span style="font-size: 23px; font-weight: bold; color:dodgerblue;">
                {{ userInfo.title }}</span><br />
            </div>
            <div style="margin-bottom: 10px;">
              <span style="">推荐:</span>
              <el-rate v-model="userInfo.rating" disabled text-color="#ff9900" score-template="{value}">
              </el-rate>
            </div>

            <span style="color: #409EFF;">类别：</span>
            <el-tag type="success" v-if="userInfo.productType == 0">出行</el-tag>
            <el-tag type="success" v-if="userInfo.productType == 1">住宿</el-tag>
            <el-tag type="success" v-if="userInfo.productType == 2">路线</el-tag>
            <el-tag type="success" v-if="userInfo.productType == 3">景点</el-tag>
            <el-table-column header-align="center" align="center" label="图片">
              <el-image :src="userInfo.imgUrl"
                style="width: 200px; height: 200px;margin-right: 0px;margin-top: 30px;"></el-image>
            </el-table-column>
            <div>
              <div class="text02Body" style="font-family: STXinwei;">
                {{ userInfo.summary }}
              </div>


            </div>

            <span>
              <img v-if="userInfo.isCollect" style=" width: 25px;height: 25px ; margin-left: 8px;"
                src="../../assets/images/icon/收藏-选中.png" @click="collected" />
              <img v-else style=" width: 25px;height: 25px ; margin-left: 8px;"
                src="../../assets/images/icon/收藏-选中 (1).png" @click="collected" />
              {{ userInfo.collects }}</span>

            <span style="margin-bottom:3px;margin-left: 12px;">
              <img v-if="userInfo.isLike" style=" width: 25px; height: 25px;" src="../../assets/images/icon/点赞.png"
                @click="liked" />
              <img v-else style=" width: 25px; height: 25px;" src="../../assets/images/icon/点赞 (1).png"
                @click="liked" />
              {{ userInfo.liked }}</span><br />
          </el-card>
        </div>
      </el-col>
    </el-row>

    <div class="comment">
      <div>
        <div class="my-reply" style="padding-top: 10px;">
          <el-avatar class="header-img" :size="50" :src="myInfo.icon"></el-avatar>
          <img src="../../assets/images/comment.png" alt="" @click="inputFocus"
            style="width: 35px;height: 35px;margin-bottom: 8px;margin-left: 10px;">
          <div class="reply-info">
            <div tabindex="0" contenteditable="true" id="replyInput" spellcheck="false" placeholder="输入评论..."
              class="reply-input" @input="onDivInput($event)" v-show="isShow"></div>
          </div>

          <div class="reply-btn-box" v-show="btnShow">
            <el-button class="reply-btn" size="medium" @click="sendComment" type="success">发表评论</el-button>
          </div>
        </div>

        <div v-for="(item, i) in comments" :key="i" class="author-title reply-father">
          <el-table-column width="620">
            <el-row>
              <div id="item">
                <div id="left">
                  <el-image :src="item.icon"
                    style=" width: 50px; height: 50px; border-radius: 100px;vertical-align: middle;"></el-image>
                </div>
                <div id="right" style="text-align: left;">
                  <div style=" color:dodgerblue;" class="name">{{ item.username }}</div>
                  <div class="content">{{ item.content }}</div>
                  <div id="time" class="time">{{ item.createTime }}</div>
                </div>
              </div>
              <el-button type="text" v-if="item.userId != myInfo.id" @click="goReplay(item.id)">回复</el-button>
              <el-button type="text" @click="delete1(item.id, item.pid)" v-if="item.userId == myInfo.id"
                style="color: red;">删除</el-button>
            </el-row>
          </el-table-column>
          <!-- 子级评论列表 -->

          <div v-for="(reply, j) in item.commentVo" :key="j" class="author-title">
            <el-table-column width="620">
              <el-row>
                <div id="top">
                  <div id="zb">
                    <el-image :src="reply.icon"
                      style=" width: 40px; height: 40px; border-radius: 100px;vertical-align: middle;"></el-image>
                  </div>
                  <div id="yb">
                    <div id="sm">
                      <div style="font-size: 18px; margin: 0 5px 0 0; color:dodgerblue;" id="mz">
                        {{ reply.username }}</div>
                      <span style="font-size: 12px;">回复</span>
                      <div style="font-size: 18px; color:dodgerblue;  margin: 0 5px;">
                        @{{ reply.toUsername }}</div>
                    </div>
                    <div class="reply" id="text">{{ reply.content }}</div>
                    <div id="sj">{{ reply.createTime }}</div>
                  </div>
                </div>
                <!-- <el-button
                  type="text"
                  @click="goReplay(reply.id)"
                  style="margin-left: 2px;"
                  >回复</el-button> -->
                <el-button type="text" v-if="reply.userId == myInfo.id" @click="delete1(reply.id, reply.pid)"
                  style="color: red;">删除</el-button>
              </el-row>
            </el-table-column>
          </div>

          <!-- 回复框 -->
          <el-dialog title="提示" :visible.sync="dialogVisible" width="30%" :before-close="handleClose">
            <el-input type="textarea" :rows="4" placeholder="请输入回复" v-model="tocomtext">
            </el-input>
            <span slot="footer" class="dialog-footer">
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="sendCommentReply(item.id)">确 定</el-button>
            </span>
          </el-dialog>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import newAxios from "../../utils/axios";
export default {
  data() {
    return {
      tableData: [],
      placeholder: "", // 回复者名称
      btnShow: false,
      index: "0",
      replyComment: "", // 评论输入内容
      username: "jaychou", // 登录的用户名
      avatar: "https://s1.ax1x.com/2022/06/10/Xc9lUf.png", // 登录用户头像
      userId: "", // 登录用户id
      parentName: "", // 回复的对象（父评论）用户名
      parentId: 0, // 父id
      itemId: "6666", // 文章等等id
      userInfo: {},
      dialogVisible: false,
      comments: [{}],
      tocomtext: "",
      collect: "",
      whoId: 0,
      replyData: [],
      myInfo: {},
      isShow: false
    };
  },
  created() {
    //获取攻略列表数据
    this.loadStrategy();
    //获取评论列表
    this.myrefresh();
    this.collect = parseInt(this.$route.query.collect);
    // this.getReply(133);
    this.getUser();
  },
  mounted() {
    //获取攻略列表数据
    this.loadStrategy();
    //获取评论列表
    this.myrefresh();
    this.collect = parseInt(this.$route.query.collect);
    // this.getReply(133);
    this.getUser();
  },
  methods: {
    getUser() {
      newAxios
        .get("/admin/findUserById")
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            console.log(res.data);
            this.myInfo = res.data;
          }
        })
        .catch((err) => {
          this.$message = "正在加载ing...有事请留言";
        });
    },
    liked() {
      newAxios
        .get("/strategy/likeStrategy", {
          params: {
            id: parseInt(this.$route.query.strategyId),
          },
        })
        .then((res) => res.data)
        .then((res) => {
          console.log(res.data);
          if (res.code == 200) {
            this.loadStrategy();
          } else {
            this.$message.error("点赞失败");
          }
        });
    },

    collected() {
      newAxios
        .get("/strategy/collectStrategy", {
          params: {
            id: parseInt(this.$route.query.strategyId),
          },
        })
        .then((res) => res.data)
        .then((res) => {
          console.log(res.data);
          if (res.code == 200) {
            this.loadStrategy();
          } else {
            this.$message.error("收藏失败");
          }
        });
    },

    toStrategy() {
      this.$router.back();
    },
    toCollect() {
      this.$router.push({
        path: "/myCollect",
      });
    },

    //获取信息
    loadStrategy() {
      // console.log("获取攻略信息");
      newAxios
        .get("/strategy/findStrategyById", {
          params: {
            id: parseInt(this.$route.query.strategyId),
          },
        })
        .then((res) => res.data)
        .then((res) => {
          console.log(res.data);
          if (res.code == 200) {
            this.userInfo = res.data;
          } else {
            // alert("获取数据失败");
            this.$message.error("数据获取失败！")
          }
        });
    },

    myrefresh() {
      newAxios
        .get("/comment/getCommentAll", {
          params: {
            foreignId: parseInt(this.$route.query.strategyId),
          },
        })
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            // let list = resp.data;
            console.log(res.data);
            this.comments = res.data;
          }
        })
        .catch((err) => {
          this.$message({
            type: "info",
            message: "查看失败！请留言联系客服",
          });
        });
    },

    inputFocus() {
      this.isShow = !this.isShow;
      this.btnShow = !this.btnShow;
      // var replyInput = document.getElementById("replyInput");
      // replyInput.style.padding = "8px 8px";
      // replyInput.style.border = "2px solid #409EFF";
      // replyInput.focus();
    },
    showReplyBtn() {
      this.btnShow = true;
    },

    sendComment() {
      // 父评论
      if (!this.replyComment) {
        this.$message({
          showClose: true,
          type: "warning",
          message: "评论不能为空",
        });
      } else {
        newAxios
          .post("/comment/publisher", {
            foreignId: parseInt(this.$route.query.strategyId),
            content: this.replyComment,
          })
          .then((res) => res.data)
          .then((res) => {
            if (res.code == 200) {
              // let list = resp.data;
              console.log(res);
              this.$message.success("发表评论成功!");
              document.getElementById("replyInput").innerHTML = "";
              this.replyComment = "";
              this.myrefresh();
            } else {
              this.$message.error("评论失败，请稍后重试！");
            }
          });
      }
    },
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then((res) => {
          done();
        })
        .catch((err) => { });
    },
    goReplay(id) {
      this.dialogVisible = true
      console.log("回复哪个：", id);
      this.whoId = id;
      console.log("whoId:", this.whoId);
      // console.log(typeof id);
    },
    sendCommentReply(id) {
      // 子回复提交
      console.log("回复哪个22：", id);
      console.log("whoId22:", this.whoId);
      newAxios
        .post("/comment/comments", {
          foreignId: parseInt(this.$route.query.strategyId),
          content: this.tocomtext,
          pid: this.whoId,
        })
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            // let list = resp.data;
            console.log(res);
            this.$message.success("评论成功!");
            this.dialogVisible = false;
            this.tocomtext = "";
            this.myrefresh();
          } else {
            this.$message.error("评论失败，请稍后重试！");
            this.dialogVisible = false;
          }
        });
    },
    delete1(id, pid) {
      console.log(id, pid);
      newAxios
        .post("/comment/detete", {
          id: id,
          pid: pid,
        })
        .then((res) => res.data)
        .then((res) => {
          if (res.code == 200) {
            console.log(res);
            this.$message.success("删除成功!");

            this.myrefresh();
          } else {
            this.$message.error("你没有权限删除他发表的评论！");
          }
        });
    },
    onDivInput: function (e) {
      this.replyComment = e.target.innerHTML;
    },
  },
};
</script>

<style>
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
}

.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n + 1) {
  background-color: #d3dce6;
}

.collect {
  display: flex;
  align-items: center;
}

.text02Body {
  text-align: left;
  font-size: 18px;
  text-indent: 2em;
  padding: 10px 10px;
  margin: 10px 0;
  text-align: center;
}

.comment {
  display: flex;
}

.reply-btn-box {
  display: inline-block;
}

.reply-info {
  width: 500px;
}

#replyInput {
  height: 80px;
  border: 2px solid #409EFF;
}

.reply-father {
  border-bottom: 1px solid #e1dede;
}

#item {
  display: flex;
}

#left {
  width: 50px;
  padding: 10px;
}

#right {
  display: flex;
  flex-direction: column;
  background-color: #fff;
  width: 400px;
}

#right .name {
  font-size: 25px;
  margin-top: 5px;
}

#right .content {
  font-size: 18px;
  line-height: 18px;
  margin: 5px 0;
}

#right .time {
  font-size: 12px;
  color: #8f8b8b;
}

#top {
  display: flex;
  justify-content: center;
  padding-top: 10px;
  margin: 5px 0;
}

#zb {
  padding: 0 5px 10px 10px;
}

#yb {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: center;
  padding-top: 5px;
  width: 300px;

}

#sm {
  display: flex;
  align-items: center;
  margin-bottom: 5px;
}

#text {
  margin-bottom: 5px;
}

#sj {
  font-size: 12px;
  color: #8f8b8b;

}
</style>
